package cn.tedu.jdbc;

import java.sql.*;
import java.util.Scanner;

public class StudentManager {
    private static Scanner scanner = new Scanner(System.in);
    private static boolean quit = true;
    private static Connection connection;

    public static void main(String[] args) {
        try {
            connection = DBUtil.getConnection();
            System.out.println();
            System.out.println();
            displayStudent();
            while (quit) {
                System.out.println("\033[35m请选择您需要的操作:\033[0m");
                System.out.print("\033[34mA.展示学生信息\033[0m ");
                System.out.print("\033[34mB.添加学生信息\033[0m ");
                System.out.print("\033[34mC.修改学生信息\033[0m ");
                System.out.print("\033[34mD.删除学生信息\033[0m ");
                System.out.print("\033[34mE.查询学生信息\033[0m ");
                System.out.println("\u001B[31mQ.退出\u001B[0m");
                String option = scanner.nextLine();
                switch (option.toUpperCase()) {
                    case "A":
                        displayStudent();
                        break;
                    case "B":
                        addStudent();
                        break;
                    case "C":
                        updateStudent();
                        break;
                    case "D":
                        deleteStudent();
                        break;
                    case "E":
                        selectStudent();
                        break;
                    case "Q":
                        quit();
                        break;
                    default:
                        System.out.println("非法选项！");
                        break;
                }
            }
            System.out.println("再见!");
        } catch (SQLException e) {
            throw new RuntimeException("出现异常，请稍后再试...", e);
        }


    }

    public static String padWithFullWidthSpaces(String str, int length) {
        int padding = length - str.length();
        StringBuilder sb = new StringBuilder(str);
        while (padding > 0) {
            sb.append((char) 12288);
            padding -= 2;
        }
        return sb.toString();
    }

    public static void displayStudent() {
        try {
            System.out.println("\033[32m**************** StudentInfo ****************\033[0m");
            System.out.println("ID\t\t" +
                    padWithFullWidthSpaces("姓名", 15 - "姓名".length()) +
                    padWithFullWidthSpaces("年龄", 14 - "年龄".length()) +
                    padWithFullWidthSpaces("性别", 10 - "性别".length()) +
                    padWithFullWidthSpaces("地址", 10 - "地址".length()));
            Statement statement = connection.createStatement();
            String sql = "select id,name,age,gender,address from student";
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                String address = resultSet.getString("address");
                System.out.println(id + "\t\t" +
                        padWithFullWidthSpaces(name, 15 - name.length()) +
                        age + "\t\t\t" +
                        padWithFullWidthSpaces(gender, 10 - gender.length()) +
                        padWithFullWidthSpaces(address, 10 - address.length()));
            }
        } catch (SQLException e) {
            throw new RuntimeException("出现异常，请稍后再试...", e);
        }
    }

    public static void addStudent() {
        try {
            System.out.println("Please input student info: name age gender address");
            String input = scanner.nextLine();
            String[] split = input.split(" ");
            String sql = "insert into student (name,age,gender,address) values (?,?,?,?);";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, split[0]);
            ps.setInt(2, Integer.parseInt(split[1]));
            ps.setString(3, split[2]);
            ps.setString(4, split[3]);
            int i = ps.executeUpdate();
            if (i > 0) {
                System.out.println("Add StudentInfo Success!");
            } else {
                System.out.println("Add StudentInfo Failed!");
            }
        } catch (SQLException e) {
            throw new RuntimeException("出现异常，请稍后再试...", e);
        }
    }

    public static void deleteStudent() {
        System.out.println("Please input the id of the student:");
        try {
            int id = Integer.parseInt(scanner.nextLine());
            PreparedStatement ps = connection.prepareStatement("delete from student where id = ?");
            ps.setInt(1, id);
            if (ps.executeUpdate() > 0) {
                System.out.println("Delete StudentInfo Success!");
            } else {
                System.out.println("Delete StudentInfo Failed!");
            }
        } catch (SQLException e) {
            throw new RuntimeException("出现异常，请稍后再试...", e);
        }
    }

    public static void updateStudent() {
        System.out.println("Please input the info of the student: id name age gender address");
        try {
            String input = scanner.nextLine();
            String[] split = input.split(" ");
            PreparedStatement ps = connection.prepareStatement("update student set name = ?,age = ?,gender = ?,address = ? where id = ?");
            ps.setString(1, split[1]);
            ps.setInt(2, Integer.parseInt(split[2]));
            ps.setString(3, split[3]);
            ps.setString(4, split[4]);
            ps.setInt(5, Integer.parseInt(split[0]));
            if (ps.executeUpdate() > 0) {
                System.out.println("Update StudentInfo Success!");
            } else {
                System.out.println("Update StudentInfo Failed!");
            }
        } catch (SQLException e) {
            throw new RuntimeException("出现异常，请稍后再试...", e);
        }
    }
    public static void selectStudent() {
        try {
            System.out.println("Please input the name of the student:");
            String n = scanner.nextLine();
            PreparedStatement ps = connection.prepareStatement("select * from student where name = ?");
            ps.setString(1, n);
            ResultSet resultSet = ps.executeQuery();
            System.out.println("\033[32m**************** StudentInfo ****************\033[0m");
            System.out.println("ID\t\t" +
                    padWithFullWidthSpaces("姓名", 15 - "姓名".length()) +
                    padWithFullWidthSpaces("年龄", 14 - "年龄".length()) +
                    padWithFullWidthSpaces("性别", 10 - "性别".length()) +
                    padWithFullWidthSpaces("地址", 10 - "地址".length()));
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                String address = resultSet.getString("address");
                System.out.println(id + "\t\t" +
                        padWithFullWidthSpaces(name, 15 - name.length()) +
                        age + "\t\t\t" +
                        padWithFullWidthSpaces(gender, 10 - gender.length()) +
                        padWithFullWidthSpaces(address, 10 - address.length()));
            }
        }catch (SQLException e){
            throw new RuntimeException("出现异常，请稍后再试...", e);
        }
    }

    public static void quit() {
        quit = false;
    }

}
